package icegps.com.pingdj.activity;


import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

import icegps.com.pingdj.R;
import icegps.com.pingdj.adapter.MapInfoAdapter;
import icegps.com.pingdj.bean.KeyValue;
import icegps.com.pingdj.map.listener.OnSaveBitmapListener;
import icegps.com.pingdj.map.utils.LatAndLot2Coord;
import icegps.com.pingdj.bean.LocationStatus;
import icegps.com.pingdj.map.bean.MapMeasureInfo;
import icegps.com.pingdj.map.bean.TestData;
import icegps.com.pingdj.map.listener.OnInfoAndConfigCallback;
import icegps.com.pingdj.map.utils.DispersionToColor;
import icegps.com.pingdj.utils.UnitUtils;

import static icegps.com.pingdj.constant.ActionKey.LOAD_TOKEN;

public class MapActivity extends BaseMapActivity implements View.OnClickListener {
    private ArrayList<KeyValue> menu;
    private MapInfoAdapter mapInfoAdapter;
    private TestData testData;
    private LatAndLot2Coord latAndLot2Coord;
    private long loadToken;

    @Override
    protected void init() {
        super.init();
        latAndLot2Coord = new LatAndLot2Coord();
        menu = new ArrayList<>();
        testData = new TestData();
        initRecycleview();
        setListener();
    }


    private void initRecycleview() {
        initLeftMenu();
        recyclerView1.setLayoutManager(new LinearLayoutManager(this));
        mapInfoAdapter = new MapInfoAdapter(R.layout.item_key_value, menu);
        recyclerView1.setAdapter(mapInfoAdapter);

    }

    @Override
    protected long getSaveToken() {
        return getIntent().getLongExtra(LOAD_TOKEN, System.currentTimeMillis());
    }

    @Override
    protected long getLoadToken() {
        loadToken = getIntent().getLongExtra(LOAD_TOKEN, System.currentTimeMillis());
        return loadToken;
    }

    private void initLeftMenu() {
        tvMainAntenna.setVisibility(View.GONE);
        tvDeputyAntenna.setVisibility(View.GONE);
        ivMainAction.setVisibility(View.GONE);
        ivDeputyAction.setVisibility(View.GONE);

        menu.add(new KeyValue(MAX_HEIGHT, initValue, heightUnit));
        menu.add(new KeyValue(MIN_HEIGHT, initValue, heightUnit));
        menu.add(new KeyValue(AVE_HEIGHT, initValue, heightUnit));
        menu.add(new KeyValue(CURRENT_HEIGHT, initValue, heightUnit));
        menu.add(new KeyValue(AREA, initValue, areaUnit));
        menu.add(new KeyValue(SOIL_VOLUME, initValue, volumeUnit));
    }


    private void setListener() {
        mapManager.setOnHeigthInfoCallback(onHeigthInfoCallback);
    }

    @Override
    protected void onResume() {
        super.onResume();
        testData.setIsDestroy(false);
        testData.getTestData(new TestData.OnTestDataListener() {
            @Override
            public void onData(LocationStatus locationStatus) {
                mapManager.refreshData(locationStatus);
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        testData.setIsDestroy(true);
        onHeigthInfoCallback = null;
    }

    @Override
    protected boolean pauseIsSaveBitmap() {
        return true;
    }

    @Override
    protected boolean backIsSaveBitmap() {
        return true;
    }


    OnInfoAndConfigCallback onHeigthInfoCallback = new OnInfoAndConfigCallback() {
        @Override
        public void onInfoAndConfig(MapMeasureInfo measureInfo) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    for (KeyValue keyValue : menu) {
                        if (keyValue.key.equals(MAX_HEIGHT)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getMax());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(MIN_HEIGHT)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getMin());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(AVE_HEIGHT)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getAve());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(SOIL_VOLUME)) {
                            String conversion = UnitUtils.conversion(UnitUtils.VOLUME, deviceStatus.getVolume(), measureInfo.getSoilVolume());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(CURRENT_HEIGHT)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getCurrentAltitude());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(AREA)) {
                            String conversion = UnitUtils.conversion(UnitUtils.AREA, deviceStatus.getArea(), measureInfo.getArea());
                            keyValue.value = conversion;
                        }
                    }
                    mapInfoAdapter.notifyDataSetChanged();
                }
            });
        }
    };

    @Override
    public void onAnalysisHexLocationData(LocationStatus locationStatus) {
        super.onAnalysisHexLocationData(locationStatus);
        locationStatus.setColor(DispersionToColor.getcolor(locationStatus.getDifference()));
        mapManager.refreshData(latAndLot2Coord.getTargetPoint(locationStatus, loadToken));
    }


}
